{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "from langchain.callbacks.base import BaseCallbackHandler\n",
    "from langchain.schema import LLMResult\n",
    "\n",
    "class MyCustomHandler(BaseCallbackHandler):\n",
    "    def on_llm_new_token(self, token:str, **kwargs) -> None:\n",
    "        print(f\"Received streaming token {token}, kwargs:{kwargs}\")\n",
    "        \n",
    "    def on_llm_end(self, response: LLMResult, **kwargs) -> None:\n",
    "        print(f\"Received non-streaming response: {response}\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Running LLMstudio Engine on http://localhost:50001 Running LLMstudio Tracking on http://localhost:50002 \n",
      "\n"
     ]
    }
   ],
   "source": [
    "from llmstudio.llm.langchain import ChatLLMstudio"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "gpt3 = ChatLLMstudio(model_id='openai/gpt-3.5-turbo', temperature=0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "from langchain.tools import tool\n",
    "from langchain.agents import AgentType, initialize_agent\n",
    "\n",
    "@tool\n",
    "def get_departure(ticket_number: str):\n",
    "    \"\"\"Use this to fetch the departure time of a train\"\"\"\n",
    "    return \"12:00 AM\"\n",
    "\n",
    "@tool\n",
    "def cancel_ticket(ticket_number: str):\n",
    "    \"\"\"Use this to cancel a ticket\"\"\"\n",
    "    return \"Ticket cancelled\"\n",
    "\n",
    "@tool\n",
    "def buy_ticket(destination: str):\n",
    "    \"\"\"Use this to buy a ticket\"\"\"\n",
    "    return \"Bought ticket number 123456\"\n",
    "\n",
    "@tool\n",
    "def make_complaint(complaint: str):\n",
    "    \"\"\"Use this to forward a complaint to the complaint department\"\"\"\n",
    "    return \"Complaint forwarded. We appreciate your feedback.\"\n",
    "\n",
    "\n",
    "def assistant(question: str)->str:\n",
    "    \"\"\"YOUR CODE HERE\"\"\"\n",
    "    tools = [get_departure, cancel_ticket, buy_ticket, make_complaint]\n",
    "\n",
    "    #rebuild agent with new tools\n",
    "    agent_executor = initialize_agent(\n",
    "        tools, gpt3, agent=AgentType.OPENAI_MULTI_FUNCTIONS, handle_parsing_errors=\"Check your output and make sure it conforms!\", verbose = True, debug = True\n",
    "    )\n",
    "\n",
    "    response = agent_executor.invoke(\n",
    "        {\n",
    "            \"input\": question\n",
    "        }\n",
    "    )\n",
    "\n",
    "    return response"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "\n",
      "\u001b[1m> Entering new AgentExecutor chain...\u001b[0m\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/Users/claudiolemos/miniconda3/envs/playground/lib/python3.9/site-packages/langchain_core/_api/deprecation.py:117: LangChainDeprecationWarning: The function `initialize_agent` was deprecated in LangChain 0.1.0 and will be removed in 0.2.0. Use Use new agent constructor methods like create_react_agent, create_json_agent, create_structured_chat_agent, etc. instead.\n",
      "  warn_deprecated(\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "INFO:     ::1:53987 - \"POST /api/tracking/logs HTTP/1.1\" 200 OK\n",
      "INFO:     ::1:53984 - \"POST /api/engine/chat/openai HTTP/1.1\" 200 OK\n",
      "\u001b[32;1m\u001b[1;3m\n",
      "Invoking: `get_departure` with `{'ticket_number': '1234'}`\n",
      "\n",
      "\n",
      "\u001b[0m\u001b[36;1m\u001b[1;3m12:00 AM\u001b[0mINFO:     ::1:53987 - \"POST /api/tracking/logs HTTP/1.1\" 200 OK\n",
      "INFO:     ::1:53989 - \"POST /api/engine/chat/openai HTTP/1.1\" 200 OK\n",
      "\u001b[32;1m\u001b[1;3mYour train departs at 12:00 AM.\u001b[0m\n",
      "\n",
      "\u001b[1m> Finished chain.\u001b[0m\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "{'input': 'When does my train depart? My ticket number is 1234',\n",
       " 'output': 'Your train departs at 12:00 AM.'}"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "assistant('When does my train depart? My ticket number is 1234')\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "llmstudio",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.18"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
